﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace App
{
    public partial class SendMail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Common.GetLoggedUser(this.Page) == null)
            {
                Response.Redirect("Login.aspx");
                return;
            }
            pnlInfo.Visible = false;
            lblTeamInfo.Text = "Vyberte členy týmů, kterým chcete e-mail odeslat.";
            lblTeamInfo.Visible = true;
            BindCheckBoxList();

        }

        protected void BindCheckBoxList()
        {
            List<AppLib.IsMember> users = MidApp.ManageIsMember.GetAllMembers(int.Parse(Request.Cookies["TeamMail"].Value));
           
            foreach (var user in users)
            {
                CheckBoxListEmails.Items.Add(user.User.Nick);
            }
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {            
               
        }

        protected void btnSend_Click(object sender, EventArgs e)
        {
            List<string> emails = new List<string>();

            foreach (ListItem item in CheckBoxListEmails.Items)
            {
                if (item.Selected)
                {
                    emails.Add(MidApp.ManageUser.GetUserByNick(item.Text).PrimaryEmail);
                }
            }

            int team = int.Parse(Request.Cookies["TeamMail"].Value);
            AppLib.User user = Common.GetLoggedUser(this.Page);
            string message = "****Uživatel " + user.Nick + ", který je členem týmu: " + MidApp.ManageTeam.GetTeamByID(team).TeamName + " vám poslal následující email:****\r\n";
            message += txtMailText.Text;
            string subject = txtEmailName.Text;
           
            try
            {
                foreach (var email in emails)
                {
                    MidApp.Mailer.Send(email, subject, message);
                }
                if (emails.Count == 0)
                {
                    pnlInfo.Visible = true;
                    lblInfo.Text = "Nevybrali jste žádné příjemce.";
                    lblTeamInfo.Visible = false;
                    CheckBoxListEmails.Items.Clear();
                    BindCheckBoxList();
                }
                else
                {
                    pnlInfo.Visible = true;
                    lblInfo.Text = "Odeslání emailů proběhlo úspěšně";
                    lblTeamInfo.Visible = false;
                    CheckBoxListEmails.Items.Clear();
                    BindCheckBoxList();
                }
            }
            catch (Exception)
            {
                pnlInfo.Visible = true;
                lblInfo.Text = "Odeslání emailů se nezdařilo";
                lblTeamInfo.Visible = false;
                CheckBoxListEmails.Items.Clear();
                BindCheckBoxList();
            }
        }
    }
}